<%@ page import="com.cinsea.util.StringUtil" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@include file="/common/common.jsp"%>
<html>
<head>
    <%@ include file="/common/head.jsp" %>
    <%
        String crmid1 = StringUtil.null2String(request.getParameter("crmid1"));
        String crmid2 = StringUtil.null2String(request.getParameter("crmid2"));
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        OrgService orgService = Env.getBean("orgService");
        HumanService humanService = Env.getBean("humanService");
        FormfieldService formfieldService = Env.getBean("formfieldService");
        List crmList = new ArrayList();
        if(!crmid1.equals("") && !crmid2.equals("")){
            String sql = "select name,code,address,shortname,post,website,email,tel,city,province,crmtype," +
            "guimo,csource,khssxy,managerid,creator,dqid,createdate,deptid,processid from ut_customerinfo where processid in(?,?)";
            crmList = jdbcDao.queryForList(sql,new Object[]{crmid1,crmid2});
        }
    %>
</head>
<body style="background-color:#dfe8f7;text-align: center">
<div id="crmDiv" style="margin-top: 5px">
        <table border="0px" id="crmTable">
            <%
                if(crmList.size()>0){
                    Map crm1Map =(Map) crmList.get(0);
                    Map crm2Map =(Map) crmList.get(1);
            %>
           <tr style="background-color: #bacfe8;height: 25px; " >
               <td  width="250px" align="center" ><b>客户信息字段</b></td>
               <td  width="360px" align="center"><b><%=crm1Map.get("name")%></b></td>
               <td  width="360px" align="center"><b><%=crm2Map.get("name")%></b></td>
           </tr>
           <tr>
               <td  width="250px">主记录</td>
               <td  width="360px"><input id="crm1" type="radio" name="mainCrm" value="<%=crm1Map.get("processid")%>"/></td>
               <td  width="360px"><input id="crm2" type="radio" name="mainCrm" value="<%=crm2Map.get("processid")%>"/></td>
           </tr>
    <%
            Object[] crm1Array = crm1Map.keySet().toArray();
            String pre = "ut_customerinfo_";
            for(int k=0; k<crm1Array.length-1; k++){
                String key = crm1Array[k].toString();
                String labelId = pre+key;
                String labelName = StringUtil.null2String(formfieldService.get(labelId).getLabelname());
                String sqlVal = "";
                String value1 = StringUtil.null2String(crm1Map.get(key));
                String value2 = StringUtil.null2String(crm2Map.get(key));
                if(key.equals("crmtype")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    optionService.get(value2).getObjname();
                    if(value1.equals("")){
                        Option option2 = optionService.get(value2);
                        value2 = option2.getObjname();
                    }else if(value2.equals("")){
                        Option option1 = optionService.get(value1);
                        value1 = option1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Option option1 = optionService.get(value1);
                        Option option2 = optionService.get(value2);
                        value1 = option1.getObjname();
                        value2 = option2.getObjname();
                    }
                }
                if(key.equals("managerid")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Human human2 = humanService.get(value2);
                        value2 = human2.getObjname();
                    }else if(value2.equals("")){
                        Human human1 = humanService.get(value1);
                        value1 = human1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Human human1 = humanService.get(value1);
                        Human human2 = humanService.get(value2);
                        value1 = human1.getObjname();
                        value2 = human2.getObjname();
                    }
                }
                if(key.equals("province")){//省，必填，不会有空值
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Option option2 = optionService.get(value2);
                        value2 = option2.getObjname();
                    }else if(value2.equals("")){
                        Option option1 = optionService.get(value1);
                        value1 = option1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Option option1 = optionService.get(value1);
                        Option option2 = optionService.get(value2);
                        value1 = option1.getObjname();
                        value2 = option2.getObjname();
                    }
                }
                if(key.equals("city")){//市，必填，不会有空值
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Option option2 = optionService.get(value2);
                        value2 = option2.getObjname();
                    }else if(value2.equals("")){
                        Option option1 = optionService.get(value1);
                        value1 = option1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Option option1 = optionService.get(value1);
                        Option option2 = optionService.get(value2);
                        value1 = option1.getObjname();
                        value2 = option2.getObjname();
                    }
                }
                if(key.equals("deptid")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Org org2 = orgService.get(value2);
                        value2 = org2.getObjname();
                    }else if(value2.equals("")){
                        Org org1 = orgService.get(value1);
                        value1 = org1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Org org1 = orgService.get(value1);
                        Org org2 = orgService.get(value2);
                        value1 = org1.getObjname();
                        value2 = org2.getObjname();
                    }
                }
                if(key.equals("creator")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Human human2 = humanService.get(value2);
                        value2 = human2.getObjname();
                    }else if(value2.equals("")){
                        Human human1 = humanService.get(value1);
                        value1 = human1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Human human1 = humanService.get(value1);
                        Human human2 = humanService.get(value2);
                        value1 = human1.getObjname();
                        value2 = human2.getObjname();
                    }
                }

                if(key.equals("dqid")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Org org2 = orgService.get(value2);
                        value2 = org2.getObjname();
                    }else if(value2.equals("")){
                        Org org1 = orgService.get(value1);
                        value1 = org1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Org org1 = orgService.get(value1);
                        Org org2 = orgService.get(value2);
                        value1 = org1.getObjname();
                        value2 = org2.getObjname();
                    }
                }
                if(key.equals("khssxy")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Option option2 = optionService.get(value2);
                        value2 = option2.getObjname();
                    }else if(value2.equals("")){
                        Option option1 = optionService.get(value1);
                        value1 = option1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Option option1 = optionService.get(value1);
                        Option option2 = optionService.get(value2);
                        value1 = option1.getObjname();
                        value2 = option2.getObjname();
                    }
                }
                if(key.equals("guimo")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Option option2 = optionService.get(value2);
                        value2 = option2.getObjname();
                    }else if(value2.equals("")){
                        Option option1 = optionService.get(value1);
                        value1 = option1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Option option1 = optionService.get(value1);
                        Option option2 = optionService.get(value2);
                        value1 = option1.getObjname();
                        value2 = option2.getObjname();
                    }
                }
                if(key.equals("csource")){
                    value1 = StringUtil.null2String(crm1Map.get(key));
                    value2 = StringUtil.null2String(crm2Map.get(key));
                    if(value1.equals("")){
                        Option option2 = optionService.get(value2);
                        value2 = option2.getObjname();
                    }else if(value2.equals("")){
                        Option option1 = optionService.get(value1);
                        value1 = option1.getObjname();
                    }
                    if(!value1.contentEquals("") && !value2.equals("")){
                        Option option1 = optionService.get(value1);
                        Option option2 = optionService.get(value2);
                        value1 = option1.getObjname();
                        value2 = option2.getObjname();
                    }
                }
    %>
                <tr>
                    <td  width="250px"><%=StringUtil.null2String(labelName)%></td>
                    <td  width="360px"><input type="radio" name="<%=key%>" value="<%=StringUtil.null2String(crm1Map.get(key))%>"/><span><%=StringUtil.null2String(value1)%></span></td>
                    <td  width="360px"><input type="radio" name="<%=key%>" value="<%=StringUtil.null2String(crm2Map.get(key))%>"/><span><%=StringUtil.null2String(value2)%></span></td>
                </tr>
            <%}}%>
            <TR style="height: 25px">
                <td colspan='3'></td>
            </TR>
                <tr>
                    <td colspan="3">
                        <input style="float:left;margin-left: 15px;width: 70px;height: 30px" type="button" value="取消" id="cancel" />
                        <input style="float:right;margin-right: 15px; width: 70px;height: 30px" type="button" value="确定" id="submit"/>
                    </td>
                </tr>
     </table>
</div>
</body>
</html>

<script type='text/javascript'>
    var mergeInfo = [];
    $("#submit").click(function(){
        var index=0;
        $("input:checked").each(function(){
            var value = $(this).val();
            var key = $(this).attr("name");
            mergeInfo[index]={'name':key,'value':value};
            index++;
        });
        var crm1 = $("#crm1").val();
        var crm2 = $("#crm2").val();
        $.ajax({
            url:"/ext/com.cinsea.crm.ext.CrmAction?action=mergeCrm&crm1="+crm1+"&crm2="+crm2,
            type:"post",
            async:true,
            data: {param:Ext.encode(mergeInfo)},
            success:function(result){
                if(result==1){
                    alert("合并成功！");
                    if(top.contentPanel)
                        try {
                            $('#notifyclose').val('0');
                            var tabpanel = top.contentPanel;
                            tabpanel.remove(tabpanel.getActiveTab());
                        } catch (e) {
                        }
                    try {
                        top.commonDialog.close();
                    } catch (e) {
                    }
                }else if(result == 0){
                    alert("合并失败！");
                    if(top.contentPanel)
                        try {
                            $('#notifyclose').val('0');
                            var tabpanel = top.contentPanel;
                            tabpanel.remove(tabpanel.getActiveTab());
                        } catch (e) {
                        }
                    try {
                        top.commonDialog.close();
                    } catch (e) {
                    }
                }
            }
        });
    });

    $("#cancel").click(function(){
        if(top.contentPanel)
            try {
                $('#notifyclose').val('0');
                var tabpanel = top.contentPanel;
                tabpanel.remove(tabpanel.getActiveTab());
            } catch (e) {
            }
        try {
            top.commonDialog.close();
        } catch (e) {
        }
    });
</script>